package cn.systemManage.action;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.basic.action.BasicAction;
import cn.systemManage.bean.UserBean;
import cn.systemManage.service.UserService;
import cn.util.CommonSendMsg;
import cn.util.ExceptionUtil;
import cn.util.PageBean;

/**
 * 用户管理
 * @author zhl
 * @since 2011-04-02
 *
 */
@Controller
@Scope("prototype")
public class UserAction extends BasicAction{
	Logger logger = Logger.getLogger(UserAction.class);
	
	private static final long serialVersionUID = 1L;
	static final String toList = "toUserList";
	static final String toEdit = "toUserEdit";

	@Resource private UserService userService;
	private UserBean userBean = new UserBean();
	private UserBean queryBean =new UserBean();

	/**
	 * to 列表页
	 * @return
	 */
	public String searchList(){
		try {
			PageBean page = this.userService.getPageBean(this.getQueryBean());
			this.putActionContext("pageData",page);
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionStack(e));
			e.printStackTrace();
		}
		return toList;
	}
	/**
	 * to  修改页
	 * @return
	 *
	 * @author  zhanghongliang
	 * @Date    2011-4-15下午01:53:49
	 */
	public String toEdit(){
		try {
			userBean = this.userService.findUserBeanById(this.getUserBean());
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionStack(e));
			e.printStackTrace();
		}
		return toEdit;
	}
	
	/**
	 * 保存用户
	 *
	 * 1  保存成功；2 用户名已经存在；-1  异常
	 *
	 * @author  zhanghongliang
	 * @Date    2011-4-15下午01:58:34
	 */
	public void saveUserBean(){
		String result = "";
		try {
			result +=this.userService.saveUserBean(this.getUserBean());
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionStack(e));
			e.printStackTrace();
			result = "-1";
		}
		CommonSendMsg.writeMsg(result);
	}
	
	/**
	 * 删除
	 * 1  成功；2 失败；-1  异常
	 *
	 * @author  zhanghongliang
	 * @Date    2011-4-15下午02:42:48
	 */
	public void deleteUserBean(){
		String result = "";
		try {
			result +=this.userService.deleteUserBean(this.getUserBean().getId());
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionStack(e));
			e.printStackTrace();
			result = "-1";
		}
		CommonSendMsg.writeMsg(result);
	}
	public UserBean getUserBean() {
		return userBean;
	}

	public UserBean getQueryBean() {
		return queryBean;
	}
}
